package com.webex.ta.hydra.gui;

import com.webex.ta.hydra.gui.table.HydraTable;
import com.webex.ta.hydra.gui.table.HydraTableMouseHandler;
import com.webex.ta.hydra.gui.util.Gutter;
import com.webex.ta.hydra.gui.util.RolloverButton;
import com.webex.ta.hydra.util.HydraUtils;

import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-19
 * Time: 16:40:29
 */
public class HydraEditorPane extends JPanel implements ActionListener, CellEditorListener {
    private HydraTable table;

    private JButton addColumn, removeColumn;

    private Gutter gutter;

    private JPanel headerPanel = new JPanel(new BorderLayout());

    public HydraEditorPane() {
        setLayout(new BorderLayout());
        table = new HydraTable();
        table.addCellEditorListener(this);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
//        table.setMinimumSize(new Dimension(100, 80));
//        table.setPreferredScrollableViewportSize(new Dimension(100, 80));
        scrollPane.setViewportView(table);
//        scrollPane.setBorder(new HydraTableBorder());
        scrollPane.addMouseListener(new HydraTableMouseHandler(table));
        add(scrollPane, BorderLayout.CENTER);

        gutter = new Gutter(table);

        addColumn = new RolloverButton(HydraUtils.getImage("include.png"));
        addColumn.setToolTipText("Add column");
        addColumn.setPreferredSize(new Dimension(14, 14));
        addColumn.setMaximumSize(new Dimension(14, 14));
        addColumn.addActionListener(this);

        removeColumn = new RolloverButton(HydraUtils.getImage("include.png"));
        removeColumn.setToolTipText("Remove column");
        removeColumn.setPreferredSize(new Dimension(14, 14));
        removeColumn.setMaximumSize(new Dimension(14, 14));
        removeColumn.addActionListener(this);

        Box box = Box.createVerticalBox();
        box.add(Box.createRigidArea(new Dimension(2,2)));
        box.add(addColumn);
        box.add(gutter);
        add(box, BorderLayout.EAST);

        add(headerPanel, BorderLayout.NORTH);
    }


    public HydraTable getTable() {
        return table;
    }

    public void setHeaderPanel(JComponent header) {
        headerPanel.removeAll();
        if (header != null)
            headerPanel.add(header, BorderLayout.CENTER);
        headerPanel.revalidate();
    }

    public void stopEdit() {
        table.stopEdit();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addColumn) {
            table.addColumn(new TableColumn(table.getColumnCount()));
        }
    }

    public void editingStopped(ChangeEvent e) {
        gutter.repaint();
    }

    public void editingCanceled(ChangeEvent e) {

    }


}
